package ru.spb.osll.myfit.server.parser;

import ru.spb.osll.myfit.common.Content;
import ru.spb.osll.myfit.common.PageElement;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ContentElementParser implements IPageElementParser {
    private final Pattern CONTENT_PATTERN = Pattern.compile("!contents\\s+-R(\\d*)\\s*");

    @Override
    public boolean matches(String line) {
        return CONTENT_PATTERN.matcher(line).matches();
    }

    @Override
    public PageElement parse(String line) {
        Matcher matcher = CONTENT_PATTERN.matcher(line);
        matcher.find();
        String level = matcher.group(1).trim();
        try {
            Integer lv;
            if (!level.equals("")) {
                lv = Integer.parseInt(level);
            } else {
                lv = null;
            }
            Content content = new Content(lv);
            return content;
        } catch (NumberFormatException e) {
        }

        return null;
    }
}
